using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Mapping.Attributes;
using Persistance.Model;
using Lib;
using System.Collections;

namespace Persistance.BLL
{
    public class SituationManagement
    {
        
        public static List<Object> getAllOtherSituations(Users author)
        {
            ISession sess = Persistor.CurrentSession;
            try
            {
                IQuery q = sess.CreateQuery("from Situation s Join s.Author as author where s.Author != :ID");
                q.SetInt32("ID", author.NumericUserID);
                List<Object> results = (List<Object>)q.List<Object>();

                return results;
            }
            catch (Exception e)
            {
                throw new Exception("Error Retrieving all Situations Not authored by " + author.FullName + ": ", e);
            }
        }

        public static List<Object> getAllSituations()
        {
            ISession sess = Persistor.CurrentSession;
            try
            {
                IQuery q = sess.CreateQuery("from Situation s Join s.Author as author");
                List<Object> results = (List<Object>)q.List<Object>();

                return results;
            }
            catch (Exception e)
            {
                throw new Exception("Error retrieving all Situations: ", e);
            }
        }

        public static List<Object> getSituationsByCustomQuery(List<String> names)
        {
            ISession sess = Persistor.CurrentSession;
            try
            {

                if (names.Count > 0)
                {
                    string query = "from Situation s Join s.Author as author where UserID = '";

                    for (int i = 0; i < names.Count; i++)
                    {
                        if (i != names.Count - 1)
                            query = query + names[i] + "' Or UserID = '";

                        else
                            query = query + names[i] + "'";
                    }

                    IQuery q = sess.CreateQuery(query);
                    List<Object> results = (List<Object>)q.List<Object>();

                    if (results != null)
                        return results;
                }

                return null;
            }

            catch (Exception e)
            {
                throw new Exception("Error Retrieving Situations: ", e);
            }
        }
    }
}
